// Web3 configuration for frontend direct blockchain access

export const WEB3_CONFIG = {
  // RPC endpoints - can use multiple for fallback
  rpcUrls: {
    bscTestnet: [
      'https://data-seed-prebsc-1-s1.binance.org:8545',
      'https://data-seed-prebsc-2-s1.binance.org:8545',
      'https://data-seed-prebsc-1-s2.binance.org:8545',
    ],
    bscMainnet: [
      'https://bsc-dataseed.binance.org',
      'https://bsc-dataseed1.defibit.io',
      'https://bsc-dataseed1.ninicoin.io',
    ],
  },

  // Query configuration
  queryConfig: {
    // Cache times in milliseconds
    staleTime: 30 * 1000, // 30 seconds - data is fresh
    gcTime: 5 * 60 * 1000, // 5 minutes - garbage collection
    
    // Retry configuration
    retry: 3,
    retryDelay: (attemptIndex: number) => Math.min(1000 * 2 ** attemptIndex, 30000),
  },

  // Direct query functions (simple reads)
  directQueries: [
    'balanceOf',
    'getBeneficiarySummary',
    'getBeneficiarySchedules',
    'getVestingSchedule',
    'totalSupply',
    'miningRate',
  ],

  // Backend API functions (complex operations)
  backendOperations: [
    'createVestingSchedule',
    'revokeVestingSchedule',
    'getAllSchedules',
    'getHistoricalData',
    'getAggregatedStats',
  ],
};

// Helper to get RPC URL with fallback
export function getRpcUrl(network: 'testnet' | 'mainnet' = 'mainnet'): string {
  const urls = network === 'testnet' 
    ? WEB3_CONFIG.rpcUrls.bscTestnet 
    : WEB3_CONFIG.rpcUrls.bscMainnet;
  
  // Return random URL for load balancing
  return urls[Math.floor(Math.random() * urls.length)];
}

// Helper to determine if should use direct query
export function shouldUseDirectQuery(functionName: string): boolean {
  return WEB3_CONFIG.directQueries.includes(functionName);
}